{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from lightrag.utils.logger import enable_library_logging\n",
    "        \n",
    "enable_library_logging(level=\"DEBUG\", enable_console=True, enable_file=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# import default config\n",
    "\n",
    "from lightrag.utils.serialization import load_json \n",
    "\n",
    "config = load_json(\"default_config.json\")\n",
    "generator_config = config[\"generator\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2024-06-17 16:24:09 - DEBUG - [_config.py:80:load_ssl_context] - load_ssl_context verify=True cert=None trust_env=True http2=False\n",
      "2024-06-17 16:24:09 - DEBUG - [_config.py:146:load_ssl_context_verify] - load_verify_locations cafile='/Users/liyin/Documents/test/LightRAG/.venv/lib/python3.11/site-packages/certifi/cacert.pem'\n",
      "2024-06-17 16:24:09 - INFO - [prompt_builder.py:79:__init__] - Prompt has variables: ['chat_history_str', 'task_desc_str', 'steps_str', 'tools_str', 'examples_str', 'output_str', 'output_format_str', 'context_str', 'input_str']\n",
      "2024-06-17 16:24:09 - INFO - [generator.py:204:call] - prompt_kwargs: {'input_str': 'how are you?'}\n",
      "2024-06-17 16:24:09 - INFO - [generator.py:205:call] - model_kwargs: {}\n",
      "2024-06-17 16:24:09 - INFO - [openai_client.py:141:call] - api_kwargs: {'model': 'gpt-3.5-turbo', 'temperature': 0, 'messages': [{'role': 'system', 'content': '<Inputs>\\nhow are you?\\n</Inputs>\\nYou:'}]}\n",
      "2024-06-17 16:24:09 - DEBUG - [_base_client.py:446:_build_request] - Request options: {'method': 'post', 'url': '/chat/completions', 'files': None, 'json_data': {'messages': [{'role': 'system', 'content': '<Inputs>\\nhow are you?\\n</Inputs>\\nYou:'}], 'model': 'gpt-3.5-turbo', 'temperature': 0}}\n",
      "2024-06-17 16:24:09 - DEBUG - [_base_client.py:949:_request] - Sending HTTP Request: POST https://api.openai.com/v1/chat/completions\n",
      "2024-06-17 16:24:09 - DEBUG - [_trace.py:45:trace] - connect_tcp.started host='api.openai.com' port=443 local_address=None timeout=5.0 socket_options=None\n",
      "2024-06-17 16:24:09 - DEBUG - [_trace.py:45:trace] - connect_tcp.complete return_value=<httpcore._backends.sync.SyncStream object at 0x12c910a50>\n",
      "2024-06-17 16:24:09 - DEBUG - [_trace.py:45:trace] - start_tls.started ssl_context=<ssl.SSLContext object at 0x12c8ecf80> server_hostname='api.openai.com' timeout=5.0\n",
      "2024-06-17 16:24:09 - DEBUG - [_trace.py:45:trace] - start_tls.complete return_value=<httpcore._backends.sync.SyncStream object at 0x11fe43e90>\n",
      "2024-06-17 16:24:09 - DEBUG - [_trace.py:45:trace] - send_request_headers.started request=<Request [b'POST']>\n",
      "2024-06-17 16:24:09 - DEBUG - [_trace.py:45:trace] - send_request_headers.complete\n",
      "2024-06-17 16:24:09 - DEBUG - [_trace.py:45:trace] - send_request_body.started request=<Request [b'POST']>\n",
      "2024-06-17 16:24:09 - DEBUG - [_trace.py:45:trace] - send_request_body.complete\n",
      "2024-06-17 16:24:09 - DEBUG - [_trace.py:45:trace] - receive_response_headers.started request=<Request [b'POST']>\n",
      "2024-06-17 16:24:10 - DEBUG - [_trace.py:45:trace] - receive_response_headers.complete return_value=(b'HTTP/1.1', 200, b'OK', [(b'Date', b'Mon, 17 Jun 2024 23:24:10 GMT'), (b'Content-Type', b'application/json'), (b'Transfer-Encoding', b'chunked'), (b'Connection', b'keep-alive'), (b'openai-organization', b'sylphai'), (b'openai-processing-ms', b'781'), (b'openai-version', b'2020-10-01'), (b'strict-transport-security', b'max-age=15724800; includeSubDomains'), (b'x-ratelimit-limit-requests', b'10000'), (b'x-ratelimit-limit-tokens', b'1000000'), (b'x-ratelimit-remaining-requests', b'9999'), (b'x-ratelimit-remaining-tokens', b'999973'), (b'x-ratelimit-reset-requests', b'6ms'), (b'x-ratelimit-reset-tokens', b'1ms'), (b'x-request-id', b'req_bca549b5dcca2db7e152d69641ae7a34'), (b'CF-Cache-Status', b'DYNAMIC'), (b'Set-Cookie', b'__cf_bm=tsyyR7kwJL8mwm2uzoxYl0ETyFkhdc58kCQ4xGvWWw0-1718666650-1.0.1.1-7NCZl_8qLI2LbgvzvV3qIyqTfixzLXgOmIgnI_kSk7YdLnGkYGiVayuOl.XhelVRZoBl8tD7HblZqkCsQ1CeNg; path=/; expires=Mon, 17-Jun-24 23:54:10 GMT; domain=.api.openai.com; HttpOnly; Secure; SameSite=None'), (b'Set-Cookie', b'_cfuvid=0MP_3H5Ly4QWn1xedKB0effi5jxNvmo52M6hth67JZc-1718666650092-0.0.1.1-604800000; path=/; domain=.api.openai.com; HttpOnly; Secure; SameSite=None'), (b'Server', b'cloudflare'), (b'CF-RAY', b'8956ca9d1d4422d2-SJC'), (b'Content-Encoding', b'gzip'), (b'alt-svc', b'h3=\":443\"; ma=86400')])\n",
      "2024-06-17 16:24:10 - INFO - [_client.py:1026:_send_single_request] - HTTP Request: POST https://api.openai.com/v1/chat/completions \"HTTP/1.1 200 OK\"\n",
      "2024-06-17 16:24:10 - DEBUG - [_trace.py:45:trace] - receive_response_body.started request=<Request [b'POST']>\n",
      "2024-06-17 16:24:10 - DEBUG - [_trace.py:45:trace] - receive_response_body.complete\n",
      "2024-06-17 16:24:10 - DEBUG - [_trace.py:45:trace] - response_closed.started\n",
      "2024-06-17 16:24:10 - DEBUG - [_trace.py:45:trace] - response_closed.complete\n",
      "2024-06-17 16:24:10 - DEBUG - [_base_client.py:988:_request] - HTTP Response: POST https://api.openai.com/v1/chat/completions \"200 OK\" Headers([('date', 'Mon, 17 Jun 2024 23:24:10 GMT'), ('content-type', 'application/json'), ('transfer-encoding', 'chunked'), ('connection', 'keep-alive'), ('openai-organization', 'sylphai'), ('openai-processing-ms', '781'), ('openai-version', '2020-10-01'), ('strict-transport-security', 'max-age=15724800; includeSubDomains'), ('x-ratelimit-limit-requests', '10000'), ('x-ratelimit-limit-tokens', '1000000'), ('x-ratelimit-remaining-requests', '9999'), ('x-ratelimit-remaining-tokens', '999973'), ('x-ratelimit-reset-requests', '6ms'), ('x-ratelimit-reset-tokens', '1ms'), ('x-request-id', 'req_bca549b5dcca2db7e152d69641ae7a34'), ('cf-cache-status', 'DYNAMIC'), ('set-cookie', '__cf_bm=tsyyR7kwJL8mwm2uzoxYl0ETyFkhdc58kCQ4xGvWWw0-1718666650-1.0.1.1-7NCZl_8qLI2LbgvzvV3qIyqTfixzLXgOmIgnI_kSk7YdLnGkYGiVayuOl.XhelVRZoBl8tD7HblZqkCsQ1CeNg; path=/; expires=Mon, 17-Jun-24 23:54:10 GMT; domain=.api.openai.com; HttpOnly; Secure; SameSite=None'), ('set-cookie', '_cfuvid=0MP_3H5Ly4QWn1xedKB0effi5jxNvmo52M6hth67JZc-1718666650092-0.0.1.1-604800000; path=/; domain=.api.openai.com; HttpOnly; Secure; SameSite=None'), ('server', 'cloudflare'), ('cf-ray', '8956ca9d1d4422d2-SJC'), ('content-encoding', 'gzip'), ('alt-svc', 'h3=\":443\"; ma=86400')])\n",
      "2024-06-17 16:24:10 - DEBUG - [_base_client.py:996:_request] - request_id: req_bca549b5dcca2db7e152d69641ae7a34\n",
      "2024-06-17 16:24:10 - INFO - [generator.py:213:call] - output: GeneratorOutput(data=\"I'm just a computer program, so I don't have feelings, but I'm here to help you with anything you need. How can I assist you today?\", error=None, usage=None, raw_response=\"I'm just a computer program, so I don't have feelings, but I'm here to help you with anything you need. How can I assist you today?\")\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "GeneratorOutput(data=\"I'm just a computer program, so I don't have feelings, but I'm here to help you with anything you need. How can I assist you today?\", error=None, usage=None, raw_response=\"I'm just a computer program, so I don't have feelings, but I'm here to help you with anything you need. How can I assist you today?\")"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from lightrag.core import Generator\n",
    "from lightrag.utils.config import construct_component\n",
    "\n",
    "\n",
    "generator = Generator(\n",
    "    model_client=construct_component(generator_config[\"model_client\"]), \n",
    "    model_kwargs=generator_config[\"model_kwargs\"])\n",
    "\n",
    "generator(prompt_kwargs={\"input_str\": \"how are you?\"})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# enable the log with file\n",
    "\n",
    "enable_library_logging(level=\"DEBUG\", enable_console=False, enable_file=True, filename=\"developer_notes/logs/lib.log\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2024-06-17 16:24:10 - INFO - [generator.py:204:call] - prompt_kwargs: {'input_str': 'how are you?'}\n",
      "2024-06-17 16:24:10 - INFO - [generator.py:205:call] - model_kwargs: {}\n",
      "2024-06-17 16:24:10 - INFO - [openai_client.py:141:call] - api_kwargs: {'model': 'gpt-3.5-turbo', 'temperature': 0, 'messages': [{'role': 'system', 'content': '<Inputs>\\nhow are you?\\n</Inputs>\\nYou:'}]}\n",
      "2024-06-17 16:24:10 - DEBUG - [_base_client.py:446:_build_request] - Request options: {'method': 'post', 'url': '/chat/completions', 'files': None, 'json_data': {'messages': [{'role': 'system', 'content': '<Inputs>\\nhow are you?\\n</Inputs>\\nYou:'}], 'model': 'gpt-3.5-turbo', 'temperature': 0}}\n",
      "2024-06-17 16:24:10 - DEBUG - [_base_client.py:949:_request] - Sending HTTP Request: POST https://api.openai.com/v1/chat/completions\n",
      "2024-06-17 16:24:10 - DEBUG - [_trace.py:45:trace] - send_request_headers.started request=<Request [b'POST']>\n",
      "2024-06-17 16:24:10 - DEBUG - [_trace.py:45:trace] - send_request_headers.complete\n",
      "2024-06-17 16:24:10 - DEBUG - [_trace.py:45:trace] - send_request_body.started request=<Request [b'POST']>\n",
      "2024-06-17 16:24:10 - DEBUG - [_trace.py:45:trace] - send_request_body.complete\n",
      "2024-06-17 16:24:10 - DEBUG - [_trace.py:45:trace] - receive_response_headers.started request=<Request [b'POST']>\n",
      "2024-06-17 16:24:11 - DEBUG - [_trace.py:45:trace] - receive_response_headers.complete return_value=(b'HTTP/1.1', 200, b'OK', [(b'Date', b'Mon, 17 Jun 2024 23:24:11 GMT'), (b'Content-Type', b'application/json'), (b'Transfer-Encoding', b'chunked'), (b'Connection', b'keep-alive'), (b'openai-organization', b'sylphai'), (b'openai-processing-ms', b'701'), (b'openai-version', b'2020-10-01'), (b'strict-transport-security', b'max-age=15724800; includeSubDomains'), (b'x-ratelimit-limit-requests', b'10000'), (b'x-ratelimit-limit-tokens', b'1000000'), (b'x-ratelimit-remaining-requests', b'9999'), (b'x-ratelimit-remaining-tokens', b'999973'), (b'x-ratelimit-reset-requests', b'6ms'), (b'x-ratelimit-reset-tokens', b'1ms'), (b'x-request-id', b'req_0a20813c5cee1be6d14d7e5f5f0baa9e'), (b'CF-Cache-Status', b'DYNAMIC'), (b'Server', b'cloudflare'), (b'CF-RAY', b'8956caa359ff22d2-SJC'), (b'Content-Encoding', b'gzip'), (b'alt-svc', b'h3=\":443\"; ma=86400')])\n",
      "2024-06-17 16:24:11 - INFO - [_client.py:1026:_send_single_request] - HTTP Request: POST https://api.openai.com/v1/chat/completions \"HTTP/1.1 200 OK\"\n",
      "2024-06-17 16:24:11 - DEBUG - [_trace.py:45:trace] - receive_response_body.started request=<Request [b'POST']>\n",
      "2024-06-17 16:24:11 - DEBUG - [_trace.py:45:trace] - receive_response_body.complete\n",
      "2024-06-17 16:24:11 - DEBUG - [_trace.py:45:trace] - response_closed.started\n",
      "2024-06-17 16:24:11 - DEBUG - [_trace.py:45:trace] - response_closed.complete\n",
      "2024-06-17 16:24:11 - DEBUG - [_base_client.py:988:_request] - HTTP Response: POST https://api.openai.com/v1/chat/completions \"200 OK\" Headers({'date': 'Mon, 17 Jun 2024 23:24:11 GMT', 'content-type': 'application/json', 'transfer-encoding': 'chunked', 'connection': 'keep-alive', 'openai-organization': 'sylphai', 'openai-processing-ms': '701', 'openai-version': '2020-10-01', 'strict-transport-security': 'max-age=15724800; includeSubDomains', 'x-ratelimit-limit-requests': '10000', 'x-ratelimit-limit-tokens': '1000000', 'x-ratelimit-remaining-requests': '9999', 'x-ratelimit-remaining-tokens': '999973', 'x-ratelimit-reset-requests': '6ms', 'x-ratelimit-reset-tokens': '1ms', 'x-request-id': 'req_0a20813c5cee1be6d14d7e5f5f0baa9e', 'cf-cache-status': 'DYNAMIC', 'server': 'cloudflare', 'cf-ray': '8956caa359ff22d2-SJC', 'content-encoding': 'gzip', 'alt-svc': 'h3=\":443\"; ma=86400'})\n",
      "2024-06-17 16:24:11 - DEBUG - [_base_client.py:996:_request] - request_id: req_0a20813c5cee1be6d14d7e5f5f0baa9e\n",
      "2024-06-17 16:24:11 - INFO - [generator.py:213:call] - output: GeneratorOutput(data=\"I'm just a computer program, so I don't have feelings, but I'm here to help you. How can I assist you today?\", error=None, usage=None, raw_response=\"I'm just a computer program, so I don't have feelings, but I'm here to help you. How can I assist you today?\")\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "GeneratorOutput(data=\"I'm just a computer program, so I don't have feelings, but I'm here to help you. How can I assist you today?\", error=None, usage=None, raw_response=\"I'm just a computer program, so I don't have feelings, but I'm here to help you. How can I assist you today?\")"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "generator(prompt_kwargs={\"input_str\": \"how are you?\"})"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "lightrag-project",
   "language": "python",
   "name": "light-rag-project"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
